﻿using System;
using System.Collections.Generic;
using System.Text;
using SecurityDAL;
using System.Data;
using Model.Security.FindEntityInfo;
using MyExection;
using Model.Security.EntityInfo;
using System.Data.SqlClient;
using BusinessLogicDAL;
using Model.BusinessLogic.FindEntityInfo;
using Model.BusinessLogic.EntityInfo;

namespace BusinessLogicBLL
{
    public class QianZaiKeHuBLL
    {
        private QianZaiKeHuDAL qianZaiKeHuDAL = null;
        private LianXiRenDAL lianXiRenDAL = null;
        public QianZaiKeHuBLL()
        {
            qianZaiKeHuDAL = new QianZaiKeHuDAL();
            lianXiRenDAL = new LianXiRenDAL();
        }

        /// <summary>
        /// 创建时间：2010-7-8
        /// 功能介绍：查询潜在用户信息
        /// </summary>
        /// <param name="FindEntity  findEntity"></param>
        /// <returns></returns>
        public DataTable SearchByFindEntity(FindQianZaiKeHuEntity findEntity)
        {
            try
            {
                return qianZaiKeHuDAL.SearchByFindEntity(findEntity);
            }
            catch (CustomException)
            {
                throw new CustomException("查询潜在用户信息出错，请重试！");
            }
        }
        /// <summary>
        /// 添加潜在用户信息
        /// </summary>
        /// <param name="entityInfo"></param>
        /// <returns></returns>
        public bool InsertQianZaiKeHu(QianZaiKeHuEntity entityInfo)
        {
            int reslut = 0;
            using (SqlConnection con = new SqlConnection(DBUtility.DBSQLHelper.defaultConnectionString))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlTransaction trans = con.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    reslut = qianZaiKeHuDAL.Add(trans, entityInfo);
                    if (reslut == 0)
                    {
                        trans.Rollback();
                        return false;
                    }
                    if (entityInfo.LianXiRenEntityList.Count > 0)
                    {
                        foreach (LianXiRenEntity lianXiRenEntity in entityInfo.LianXiRenEntityList)
                        {
                            lianXiRenEntity.CustomerID = reslut;
                            if (lianXiRenDAL.Add(trans, lianXiRenEntity) == 0)
                            {
                                trans.Rollback();
                                return false;
                            }
                        }
                    }
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 创建人：
        /// 创建时间：2010-7-8
        /// 功能介绍：根据“key”查询QianZaiKeHuEntity
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public QianZaiKeHuEntity GetListDetailById(string key)
        {
            try
            {
                return qianZaiKeHuDAL.GetListDetailById(key);
            }
            catch (CustomException)
            {
                throw new CustomException("查询潜在用户信息出错，请重试！");
            }
        }
        /// <summary>
        /// 获得LianXiRen DataTable
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public DataTable GetLianXiRenDTByCustomerID(int CustomerID)
        {
            try
            {
                return lianXiRenDAL.GetLianXiRenDTByCustomerID(CustomerID);
            }
            catch (CustomException)
            {
                throw new CustomException("查询潜在用户信息出错，请重试！");
            }
        }
        /// <summary>
        /// 更新潜在客户
        /// </summary>
        /// <param name="entityInfo"></param>
        /// <returns></returns>
        public bool UpdateQianZaiKeHu(QianZaiKeHuEntity entityInfo)
        {
            bool flag = false;
            using (SqlConnection con = new SqlConnection(DBUtility.DBSQLHelper.defaultConnectionString))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlTransaction trans = con.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
                try
                {
                    flag = qianZaiKeHuDAL.Update(trans, entityInfo);
                    if (!flag)
                    {
                        trans.Rollback();
                        return false;
                    }
                    flag = lianXiRenDAL.Delete(trans, entityInfo.CustomerID.ToString());
                    if (!flag)
                    {
                        trans.Rollback();
                        return false;
                    }
                    if (entityInfo.LianXiRenEntityList.Count > 0)
                    {
                        foreach (LianXiRenEntity lianXiRenEntity in entityInfo.LianXiRenEntityList)
                        {
                            lianXiRenEntity.CustomerID = entityInfo.CustomerID;
                            if (lianXiRenDAL.Add(trans, lianXiRenEntity) == 0)
                            {
                                trans.Rollback();
                                return false;
                            }
                        }
                    }
                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    return false;
                }
            }
            return true;
        }
    }
}
